package info.emamian.droplet.worker.controllers;

import info.emamian.droplet.data.DynamoDbClientFactory;
import info.emamian.droplet.data.DynamoDbSms;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;

@Controller
public class HomeController {
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

	public HomeController() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		logger.info("Welcome home! The client locale is {}.", locale);

		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

		String formattedDate = dateFormat.format(date);

		model.addAttribute("serverTime", formattedDate + " --  " + System.getProperty("myregion") + "--"
				+ System.getProperties().toString());

		return "home";
	}

	@RequestMapping(value = "/load", method = RequestMethod.GET)
	public @ResponseBody
	String load(HttpServletRequest request, @RequestParam(value = "id") String id) {
		DynamoDBMapper mp = new DynamoDBMapper(DynamoDbClientFactory.getClientInstance());
		DynamoDbSms sms = mp.load(DynamoDbSms.class, id);
		return sms.toString();
	}

	@RequestMapping(value = "/check", method = RequestMethod.GET)
	public void check(HttpServletResponse response) {
		response.setStatus(HttpStatus.OK.value());
	}

}
